<template>
  <t-dialog :header="`${header} 审批、评审记录`" :visible.sync="visible" :onClose="closeDialog"  :closeOnOverlayClick="false"
            :confirmBtn="null" width="80%"
            top="5%">
    <div style="height: 65vh">
      <t-tabs v-model="tabValue" @change="handleChange">
        <t-tab-panel value="1" label="审批记录" :destroyOnHide="true">
          <t-table
            rowKey="id"
            :data="data"
            :columns="columns"
            hover
            :loading="dataLoading"
          >
            <template #approveType="{row}">
              {{ row.approveType|filterByDict(dict.type.kjsf_approve_type) }}
            </template>
            <template #approveStatus="{row}">
              <t-tag v-if="row.approveStatus"
                     :theme="handleTheme(row.approveStatus, dict.type.approve_status )" variant="light">
                {{ row.approveStatus | filterByDict(dict.type.approve_status ) }}
              </t-tag>
              <t-tag v-else theme="warning" variant="light">待审批
              </t-tag>
            </template>
          </t-table>
        </t-tab-panel>
        <t-tab-panel value="2" class="tab-panel" label="立项评审记录" :destroyOnHide="true">
          <t-alert style="margin-bottom: 10px" theme="info" v-if="reviewGroupRecord?.content" :title="`${site_tag==='jtt'?'科教处':'科信部'}意见`" :message="reviewGroupRecord?.content">
          </t-alert>
          <t-alert v-else theme="info" style="margin-bottom: 10px"  :title="`${site_tag==='jtt'?'科教处':'科信部'}意见`" message="未提交" >
          </t-alert>
          <t-table
            rowKey="id"
            :data="data2"
            :columns="columns2"
            hover
          >
            <template #sysAttMainEntities="{row}">
              <a class="t-button-link" v-for="(item,index) in row.sysAttMainEntities" :key="index" target="_blank" :href="item.fullFilePath">
                {{item.fileName}}
              </a>
            </template>
            <template #reviewStatus="{row}">
              <t-tag theme="success" variant="light" v-if="row.reviewStatus==='1'">
                已提交
              </t-tag>
              <t-tag  v-else-if="row.reviewStatus === '0'" theme="warning" variant="light">暂存</t-tag>
              <t-tag theme="danger" variant="light" v-else>
                未提交
              </t-tag>
            </template>
            <template #id="{ rowIndex }">
              {{rowIndex+1}}
            </template>
            <template #[`experts.sysUser.duty`]="{ row }">
              <div v-if="['99999',''].includes(row.experts?.sysUser?.profTit)">
                {{ row.experts?.sysUser?.duty }}
              </div>
              <div v-else>
                {{ row.experts?.sysUser?.duty }}/{{ row.experts?.sysUser?.profTit | filterByDict(dict.type.prof_tit) }}
              </div>
            </template>
            <template #op="{ row }">
              <t-button v-if="row.projectApproval==='1'"
                        variant="text" class="t-button-link" @click="dialog.reviewType=row.reviewType;dialog.teamId=row.teamId;dialog.objId=row.id;dialog.open=true">
                <t-icon name="info-circle" slot="icon"/>
                详情
              </t-button>
            </template>
            <template #recommendType="{row}">
              <t-tag theme="danger" variant="light" v-if="row.recommendType==='1'">
                {{ row.recommendType | filterByDict(dict.type.recommend_type)}}
              </t-tag>
              <t-tag theme="success" variant="light" v-else-if="row.recommendType==='2'">
                {{ row.recommendType | filterByDict(dict.type.recommend_type)}}
              </t-tag>
              <t-tag theme="success" variant="light" v-else-if="row.recommendType==='3'">
                {{ row.recommendType | filterByDict(dict.type.recommend_type)}}
              </t-tag>
              <t-tag theme="warning" v-else  variant="light">
                未提交
              </t-tag>
            </template>
            <!--          <template #projectApproval="{row}">-->
            <!--            <t-tag theme="success" v-if="row.projectApproval==='1'">-->
            <!--              是-->
            <!--            </t-tag>-->
            <!--            <t-tag theme="danger" v-else>-->
            <!--              否-->
            <!--            </t-tag>-->
            <!--          </template>-->
            <template #[`experts.sysUser.education`]="{ row }">
              {{ row.sysUser?.education | filterByDict(dict.type.education) }}
            </template>
            <template #[`experts.sysUser.degree`]="{ row }">
              {{ row.sysUser?.degree | filterByDict(dict.type.degree) }}
            </template>
          </t-table>

        </t-tab-panel>
        <t-tab-panel value="3" class="tab-panel" label="实施方案评审记录" :destroyOnHide="true">
          <!--        </div>-->
          <t-alert style="margin-bottom: 10px" theme="info" v-if="reviewGroupRecord?.content" :title="`${site_tag==='jtt'?'科教处':'科信部'}意见`" :message="reviewGroupRecord?.content">
          </t-alert>
          <t-alert v-else theme="info" style="margin-bottom: 10px"  :title="`${site_tag==='jtt'?'科教处':'科信部'}意见`" message="未提交" >
          </t-alert>
          <t-alert style="margin-bottom: 10px" theme="info" v-if="psYjsFilesNow.length>0" title="专家组评审意见表" >
            <template #message>
              <a  class="t-button-link" target="_blank"  :href="psYjsFilesNow[0].url" >
                {{psYjsFilesNow[0].name}}
              </a>
            </template>
          </t-alert>
          <t-alert v-else theme="info" style="margin-bottom: 10px"  title="专家组评审意见表" message="未提交" >
          </t-alert>
          <t-table
            rowKey="id"
            :data="data2"
            :columns="columns3"
            hover
          >
            <template #sysAttMainEntities="{row}">
              <a class="t-button-link" v-for="(item,index) in row.sysAttMainEntities" :key="index" target="_blank" :href="item.fullFilePath">
                {{item.fileName}}
              </a>
            </template>
            <template #id="{ rowIndex }">
              {{rowIndex+1}}
            </template>
            <template #reviewStatus="{row}">
              <t-tag theme="success" variant="light" v-if="row.reviewStatus==='1'">
                已提交
              </t-tag>
              <t-tag  v-else-if="row.reviewStatus === '0'" theme="warning" variant="light">暂存</t-tag>
              <t-tag theme="danger" variant="light" v-else>
                未提交
              </t-tag>
            </template>
            <template #[`experts.sysUser.duty`]="{ row }">
              <div v-if="['99999',''].includes(row.experts?.sysUser?.profTit)">
                {{ row.experts?.sysUser?.duty }}
              </div>
              <div v-else>
                {{ row.experts?.sysUser?.duty }}/{{ row.experts?.sysUser?.profTit | filterByDict(dict.type.prof_tit) }}
              </div>
            </template>
            <template #op="{ row }">
              <t-button v-if="row.projectApproval==='1'"
                        variant="text" class="t-button-link" @click="dialog.reviewType=row.reviewType;dialog.teamId=row.teamId;dialog.objId=row.id;dialog.open=true">
                <t-icon name="info-circle" slot="icon"/>
                详情
              </t-button>
            </template>
            <template #projectApproval="{row}">
              <t-tag theme="success" variant="light" v-if="row.projectApproval==='1'">
                是
              </t-tag>
              <t-tag theme="danger" variant="light" v-else-if="row.projectApproval==='0'">
                否
              </t-tag>
              <t-tag theme="warning" variant="light" v-else-if="row.projectApproval==='2'">
                需要复议
              </t-tag>
              <t-tag theme="warning" variant="light" v-else>
                未提交
              </t-tag>
            </template>
            <template #[`experts.sysUser.education`]="{ row }">
              {{ row.sysUser?.education | filterByDict(dict.type.education) }}
            </template>
            <template #[`experts.sysUser.degree`]="{ row }">
              {{ row.sysUser?.degree | filterByDict(dict.type.degree) }}
            </template>
          </t-table>
        </t-tab-panel>
          <t-tab-panel value="4" class="tab-panel" label="验收评审记录" :destroyOnHide="true">
        <t-alert style="margin-bottom: 10px" theme="info" v-if="reviewGroupRecord?.content" :title="`${site_tag==='jtt'?'科教处':'科信部'}意见`" :message="reviewGroupRecord?.content">
        </t-alert>
        <t-alert v-else theme="info" style="margin-bottom: 10px"  :title="`${site_tag==='jtt'?'科教处':'科信部'}意见`" message="未提交" >
        </t-alert>
        <t-alert style="margin-bottom: 10px" theme="info" v-if="psYjsFilesNow.length>0" title="专家组评审意见表" >
          <template #message>
            <a  class="t-button-link" target="_blank"  :href="psYjsFilesNow[0].url" >
              {{psYjsFilesNow[0].name}}
            </a>
          </template>
        </t-alert>
        <t-alert v-else theme="info" style="margin-bottom: 10px"  title="专家组评审意见表" message="未提交" >
        </t-alert>
        <t-table
          rowKey="id"
          :data="data2"
          :columns="columns3"
          hover
        >
          <template #sysAttMainEntities="{row}">
            <a class="t-button-link" v-for="(item,index) in row.sysAttMainEntities" :key="index" target="_blank" :href="item.fullFilePath">
              {{item.fileName}}
            </a>
          </template>
          <template #id="{ rowIndex }">
            {{rowIndex+1}}
          </template>
          <template #reviewStatus="{row}">
            <t-tag theme="success" variant="light" v-if="row.reviewStatus==='1'">
              已提交
            </t-tag>
            <t-tag  v-else-if="row.reviewStatus === '0'" theme="warning" variant="light">暂存</t-tag>
            <t-tag theme="danger" variant="light" v-else>
              未提交
            </t-tag>
          </template>
          <template #[`experts.sysUser.duty`]="{ row }">
            <div v-if="['99999',''].includes(row.experts?.sysUser?.profTit)">
              {{ row.experts?.sysUser?.duty }}
            </div>
            <div v-else>
              {{ row.experts?.sysUser?.duty }}/{{ row.experts?.sysUser?.profTit | filterByDict(dict.type.prof_tit) }}
            </div>
          </template>
          <template #op="{ row }">
            <t-button v-if="row.projectApproval==='1'"
                      variant="text" class="t-button-link" @click="dialog.reviewType=row.reviewType;dialog.teamId=row.teamId;dialog.objId=row.id;dialog.open=true">
              <t-icon name="info-circle" slot="icon"/>
              详情
            </t-button>
          </template>
          <template #projectApproval="{row}">
<!--            <t-tag theme="success" variant="light" v-if="row.projectApproval==='1'">-->
<!--              是-->
<!--            </t-tag>-->
<!--            <t-tag theme="danger" variant="light" v-else-if="row.projectApproval==='0'">-->
<!--              否-->
<!--            </t-tag>-->
<!--            <t-tag theme="warning" variant="light" v-else-if="row.projectApproval==='2'">-->
<!--              需要复议-->
<!--            </t-tag>-->
<!--            <t-tag theme="warning" variant="light" v-else>-->
<!--              未提交-->
<!--            </t-tag>-->
            <t-tag v-if="row.projectApproval"
                   :theme="handleTheme(row.projectApproval, dict.type.kjxm_ys_review_status)" variant="light">
              {{ row.projectApproval | filterByDict(dict.type.kjxm_ys_review_status) }}
            </t-tag>
            <t-tag  v-else theme="warning" variant="light">未提交
            </t-tag>
          </template>
          <template #[`experts.sysUser.education`]="{ row }">
            {{ row.experts?.sysUser?.education | filterByDict(dict.type.education) }}
          </template>
          <template #[`experts.sysUser.degree`]="{ row }">
            {{ row.experts?.sysUser?.degree | filterByDict(dict.type.degree) }}
          </template>
        </t-table>
      </t-tab-panel>
      </t-tabs>
    </div>
  </t-dialog>
</template>

<script>
import {getFiles} from "@/api/common";

export default {
  name: "approve-list",
  dicts:["prof_tit","duty","recommend_type","kjsf_approve_type", "approve_status","approve_type","subject_classification","kjxm_ys_review_status"],
  components:{

  },
  props:['header',"objId","projectName","reportTrialId"],
  data(){
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      psYjsFilesNow: [],
      data3:[],
      tabValue:"1",
      dialog:{
        objId:0,
        teamId:0,
        open:false,
        reviewType: null,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approveType: '',
        businessName: '',
      },
      visible: true,
      data: [],
      columns: [
        // {
        //   colKey: "businessId",
        //   title: "业务id",
        //   align: "center"
        // },
        {
          colKey: "businessName",
          title: "业务名称",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "approveType",
          title: "审批业务类型",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "approverName",
          title: "审批人",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "deptName",
          title: "单位",
          align: "center",
        },
        {
          colKey: "opinion",
          title: "审批意见",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "approveStatus",
          title: "审批结果",
          align: "center",
        },
        {
          colKey: "updateTime",
          title: "审批时间",
          align: "center",
          width: 200
        },
      ],

      dataLoading: false,
      data4:[],

      data2:[],
      // data2:[],
      // data2:[],
      columns2:[
        {
          colKey: "id",
          title: "专家序号",
          align: "center",
          width: 100,
        },
        // {
        //   colKey: "experts.sysUser.dept.deptName",
        //   title: "所在单位",
        //   align: "center"
        // },
        // {colKey: 'experts.sysUser.duty', title: '职务/职称',align: "center"},
        // // {colKey: 'experts.profTit', title: '职称',align: "center"},
        // {colKey: 'experts.sysUser.majorName', title: '专业',align: "center"},
        // {colKey: 'experts.sysUser.majorNowName', title: '现从事专业',align: "center"},
        // {
        //   colKey: "experts.sysUser.education",
        //   title: "学历",
        //   align: "center",
        //   ellipsis: true
        // },
        // {
        //   colKey: "experts.sysUser.degree",
        //   title: "学位",
        //   align: "center",
        //   ellipsis: true
        // },
        {colKey: 'reviewStatus', title: '评审状态',align: "center", width: 100},
        {colKey: 'score', title: '评审分数',align: "center", width: 100},
        {colKey: 'content', title: '评审意见',align: "center", width: 300},
        {colKey: 'sysAttMainEntities', title: '评审附件',align: "center", width: 120},
        // {colKey: 'projectApproval', title: '是否通过',align: "center"},
        {colKey: 'recommendType', title: '是否推荐',align: "center", width: 100},
        // {
        //   colKey: "op",
        //   title: "操作",
        //   align: "center"
        // },
      ],
      columns3:[
        {
          colKey: "id",
          title: "专家序号",
          align: "center",
          width: 100,
        },
        {colKey: 'reviewStatus', title: '评审状态',align: "center", width: 100},
        {colKey: 'content', title: '评审意见',align: "center",width: 300},
        {colKey: 'sysAttMainEntities', title: '评审附件',align: "center", width: 120},
        // {colKey: 'score', title: '评审分数',align: "center"},
        // {colKey: 'projectApproval', title: '是否通过',align: "center"},
        {colKey: 'projectApproval', title: '是否通过',align: "center", width: 100},
      ],
      reviewGroupRecord: null,
    }
  },
  async created() {
    this.getList();
    this.getKjcReply();
    this.getReviewList();
  },
  methods:{
    handleChange(val){
      this.reviewGroupRecord = null;
      this.getKjcReply();
      this.getReviewList();
    },
    getKjcReply(){
      if(Number(this.tabValue)>1){
        this.$api.kjsf.review.projectReviewTeamGetByBusinessIdAndReviewType({
          businessId:this.objId,
          reviewType:(Number(this.tabValue)-1).toString(),
        }).then(res => {
          this.reviewGroupRecord = res.data;
        }).catch((e) => {
          this.$message.error(e.toString());
        });
      }
    },
    getReviewList(){
      this.data2 = [];
      if(Number(this.tabValue)>1){
        this.$api.kjsf.review.projectExpertsInfoByReviewType({
          projectId:this.objId,
          reviewType: (Number(this.tabValue)-1).toString(),
        }).then(response => {
          this.data2 = response.data || [];
          this.psYjsFilesNow = [];
          if(Number(this.tabValue)>2 && this.data2.length>0){
            this.$api.kjsf.review.getByTeamIdAndBusinessId(this.objId,this.data2[0].teamId).then(res => {
              getFiles(res.data.id).then((res) => {
                const files = {}
                res.data.forEach((item) => {
                  if (!files[item.moduleKey]) {
                    files[item.moduleKey] = []
                  }
                  files[item.moduleKey].push({
                    name: item.fileName,
                    url: item.fullFilePath,
                    id: item.id,
                    filePath: item.filePath
                  })
                })
                this.psYjsFilesNow = files.dgAndYsPsYjs || [];
              }).catch(e => {
                this.$message.error(e.toString())
              })
            }).catch((e) => {
              this.$message.error(e.toString());
            });
          }
        }).catch((e) => {
          this.$message.error(e.toString());
        });
      }

    },
    getList() {
      this.dataLoading = true;
      if(this.objId){
        this.$api.kjsf.project.getByBusinessId(this.objId).then(response=>{
          this.data = response.data;
          this.dataLoading = false;
        }).catch((e) => {
          this.dataLoading = false;
          this.$message.error(e.toString());
        });
      }
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style lang="less" scoped>
.t-tab-panel{
  padding: 10px;
}
</style>
